import axios from 'axios'
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'


const request = axios.create({
    baseURL: "http://localhost:3000/api",
    timeout: 5000
})


request.interceptors.request.use(config => {
    nprogress.start()

    // 添加Token到请求中
    const user = JSON.parse(sessionStorage.getItem('user'))
    if(user){
        let token = user.token
        config.headers.token = token
    }
    return config
})


request.interceptors.response.use(response => {
    nprogress.done()
    return response.data
}, err => {
    console.log(err)
})

export default request